package com.wu.gumomallmember.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wu.common.vo.member.MemberUserLoginVo;
import com.wu.common.vo.member.MemberUserRegisterVo;
import com.wu.common.entity.member.MemberLevelEntity;
import com.wu.gumomallmember.member.service.MemberLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.common.utils.PageUtils;
import com.wu.common.utils.Query;

import com.wu.gumomallmember.member.dao.MemberDao;
import com.wu.common.entity.member.MemberEntity;
import com.wu.gumomallmember.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelService memberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public MemberEntity register(MemberUserRegisterVo memberUserRegisterVo)  throws RuntimeException {
        MemberEntity memberEntity = getByPhone(memberUserRegisterVo.getPhone());
        if (memberEntity !=null){
            throw new RuntimeException("手机号已经注册");
        }
        memberEntity = getByUserName(memberUserRegisterVo.getUserName());
        if (memberEntity !=null){
            throw new RuntimeException("昵称已经存在");
        }
        //设置默认等级
        MemberLevelEntity levelEntity = memberLevelService.getDefaultLevel();
        memberEntity = new MemberEntity();
        memberEntity.setLevelId(levelEntity.getId());
        memberEntity.setMobile(memberUserRegisterVo.getPhone());
        memberEntity.setUsername(memberUserRegisterVo.getUserName());
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        memberEntity.setPassword(bCryptPasswordEncoder.encode(memberUserRegisterVo.getPassword()));
        memberEntity.setGender(0);
        memberEntity.setCreateTime(new Date());
        this.save(memberEntity);
        return memberEntity;
    }

    @Override
    public MemberEntity getByPhone(String phone){
        return this.getOne(new LambdaQueryWrapper<MemberEntity>().eq(MemberEntity::getMobile, phone));
    }

    @Override
    public MemberEntity getByUserName(String userName){
        return this.getOne(new LambdaQueryWrapper<MemberEntity>().eq(MemberEntity::getUsername, userName));
    }
    @Override
    public MemberEntity getByLoginName(String loginName){
        return this.getOne(new LambdaQueryWrapper<MemberEntity>().eq(MemberEntity::getUsername, loginName).or().eq(MemberEntity::getMobile, loginName));
    }

    @Override
    public MemberEntity login(MemberUserLoginVo memberUserLoginVo)  throws RuntimeException  {
        MemberEntity byLoginName = this.getByLoginName(memberUserLoginVo.getLoginacct());
        if (byLoginName == null){
            throw new RuntimeException("账号不存在");
        }
        if (new BCryptPasswordEncoder().matches(memberUserLoginVo.getPassword(), byLoginName.getPassword())){
            return byLoginName;
        }else {
            throw new RuntimeException("账号密码不正确");
        }
    }

}